//package com.j.lemon.learn.basic.aspects;
//
//import com.j.lemon.learn.basic.annotations.Limiter;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.Signature;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.core.script.DefaultRedisScript;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.lang.reflect.Method;
//import java.util.Collections;
//import java.util.List;
//
///**
// * @Author lijunjun
// * @Date 2018-12-26 10:42
// * @Description
// */
//@Component
//@Aspect
//public class LuaRateLimiterAspect {
//    private static final Logger logger = LoggerFactory.getLogger(LuaRateLimiterAspect.class);
//    @Resource
//    private RedisTemplate<String,Long> redisTemplate;
//
//    @Resource
//    private DefaultRedisScript<Long> redisScript;
//
//    @Pointcut(value = "@annotation(limiter)",argNames = "limiter")
//    public void limit(Limiter limiter){}
//
//    @Before(value = "com.j.lemon.learn.basic.aspects.LuaRateLimiterAspect.limit(limiter)")
//    public void before(JoinPoint joinPoint ,Limiter limiter){
//        Signature signature = joinPoint.getSignature();
//        MethodSignature methodSignature = (MethodSignature)signature;
//        Method target = methodSignature.getMethod();
//        String methodName = target.getName();
//        List<String> keys = Collections.singletonList(methodName);
//        int limit = limiter.limit();
//        int sec = limiter.sec();
//        Long number = null;
//        try {
//            number = redisTemplate.execute(redisScript, keys, Long.valueOf(String.valueOf(limit)), sec);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        if (number != null && number.intValue() != 0 && number.intValue() <= limit) {
//            logger.info("限流时间段内访问第：{} 次", number.toString());
//            return;
//        }
//        System.out.println("访问过于频繁");
//    }
//}
